from django.db.models import Sum

from utils import contants


def shop_cart(request):
    '''当前用户购物车信息'''
    user = request.user
    cart_list = []
    cart_total = {}
    cart_count = 0
    if user.is_authenticated:
        cart_list = user.carts.filter(
            status=contants.ORDER_STATUS_INIT
        )
        cart_total = cart_list.aggregate(sum_amount=Sum('amount'), sum_count=Sum('count'))
        cart_count = cart_list.count()

    return {
        'cart_count': cart_count,  # 购物车中的商品总数
        'cart_list': cart_list,
        'cart_total': cart_total,
    }